$(function(){
	$('#search-objects-form').submit(function(){
		var val = $('select[name="country"]').val();
		if (val == 0) return false;
		return true;
	});
	$('#extended_filter').hide();
	$("#show_more_filters").click(function() {
		if ($("#extended_filter").is(':visible')) {
			$("#extended_filter").animate({height: '0px'}, 1000, function(){$(this).hide();});
		}
		else {
			$("#extended_filter").show().animate({height: '67px'}, 1000);
		}
	});
	$('.search_type a').click(function(){
		$('input[name="sale_type"]').val($(this).attr('rel'));
		$('a', $(this).parent()).removeClass('active');
		$(this).addClass('active');
	});
});

function gotoPage(i) {
	$('input[name="page"]').val(i);
	$('#search-objects-form').trigger('submit');
}


function sb_changeCountry(select) {
	var val = $(select).val();
	$('select[name="region"] option').each(function(){if ($(this).val() > 0) $(this).remove();});
	$('select[name="city"] option').each(function(){if ($(this).val() > 0) $(this).remove();});
	$('select[name="location"] option').each(function(){if ($(this).val() > 0) $(this).remove();});
	if (val != 0) {
		$.ajax({
			type: 'POST',
			url: HTTP_HOST + '/index/get-regions-list',
			data: {id: val},
			success: function(data, textStatus){showRegions(data);},
			dataType: 'json'
		});
	}
}

function showRegions(r) {
	$('select[name="region"] option:eq(0)').remove();
	$('select[name="city"]').children().remove();
	for (i in r.regions) {
		var opt = $('<option/>').val(i).html(r.regions[i].Name);
		$('select[name="region"]').append(opt);
	}
	var regid = -1;
	var optgroup = null;
	for (i in r.cities) {
		city = r.cities[i];
		if (city.RegionID != regid) {
			regid = city.RegionID;
			if (optgroup != null) {
				$('select[name="city"]').append(optgroup);
				optgroup = null;
			}
			if (city.RegionID == 0) {
				//just add the option
				var opt = $('<option/>').val(i).html(city.Name);
				$('select[name="city"]').append(opt);
			}
			else {
				optgroup = $('<optgroup />').attr('label', city.RName).attr('class', regid);
				var opt = $('<option/>').val(i).html(city.Name);
				optgroup.append(opt);
			}
		}
		else {
			var opt = $('<option/>').val(i).html(city.Name);
			optgroup.append(opt);
		}
	}
	if (optgroup != null) {
		$('select[name="city"]').append(optgroup);
		optgroup = null;
	}
}

function sb_changeRegion(select) {
	var val = $(select).val();
	//$('select[name="city"] option').each(function(){if ($(this).val() > 0) $(this).remove();});
	$('select[name="location"] option').each(function(){if ($(this).val() > 0) $(this).remove();});
	var found = false;
	if (val != 0) {
		if ($('select[name="city"] optgroup.' + val).length > 0) {
			$('option:eq(0)', $('select[name="city"] optgroup.' + val)).attr('selected', true);
			found = true;
		}
	}
	if (found === false) {
		$('select[name="city"] option[value="0"]').attr('selected', true);
	}
	$('select[name="city"]').trigger('change');
}

function sb_changeCity(select) {
	var val = $(select).val();
	$('select[name="location"] option').each(function(){if ($(this).val() > 0) $(this).remove();});
	if (val != 0) {
		$('select[name="region"] option[value="' + $('option:selected', select).parent().attr('class') + '"]').attr('selected', true);
		$.ajax({
			type: 'POST',
			url: HTTP_HOST + '/index/get-locations-list',
			data: {id: val},
			success: function(data, textStatus){showLocations(data);},
			dataType: 'json'
		});
	}
}

function showLocations(r) {
	$('select[name="location"] option:eq(0)').remove();
	for (i in r) {
		var opt = $('<option/>').val(i).html(r[i].Name);
		$('select[name="location"]').append(opt);
	}
}

function makeSearch(params) {
	var def = {
		'sale_type':1,
		'country':0,
		'region':0,
		'city':0,
		'location':0,
		'object_type':0,
		'object_price_low':0,
		'object_price_high':0,
		'object_see_distance':0,
		'object_air_distance':0,
		'object_year':0,
		'object_area_low':0,
		'object_area_high':0,
		'object_bedrooms_low':0,
		'object_bedrooms_high':0,
		'SwimmingPool':0,
		'Security':0,
		'Parking':0,
		'object_id':0,
		'page':1
	};
	jQuery.extend( def, params );
	for (i in def) {
		$('#search-objects-form-hidden').append($('<input />').attr('type', 'hidden').attr('name', i).val(def[i]));
	}
	$('#search-objects-form-hidden').trigger('submit');
}